package com.ensentec.Lib;

import android.util.Log;

/**
 * Created by ztlchina on 17-8-2.
 */

public class  EnsenLib{
    private static final String TAG = "EnsenLib";

    private static final EnsenLib ourInstance = new EnsenLib();
    private boolean   isStop = false;
    private ReadCardNumThread mReadCardThread;



    public static EnsenLib getInstance() {

//        if (ourInstance == null)
//        {
//            ourInstance = new EnsenLib();
//        }

        return ourInstance;
    }

    private EnsenLib() {
        try {

            mReadCardThread = new ReadCardNumThread();

            isStop = false;
            mReadCardThread.start();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // Used to load the 'native-lib' library on application startup.
    static {
        System.loadLibrary("native-lib");
    }

    public int getWeight () throws EnsenLibException, OverWeightException
    {
        int weight = ElecScaleReadValue(3);
        switch (weight)
        {
            case -1:
                throw  new EnsenLibException("串口打开失败");
            case -2:
                throw  new EnsenLibException("串口设置失败");
            case -3:
            case -4:
                throw  new EnsenLibException("串口通信异常");
            case -10:
                throw  new EnsenLibException("参数设置错误");
        }

        if (weight > 50000)
            throw  new OverWeightException("电子秤称重超重50Kg");

        return weight;
    }

    public void setBalance() throws EnsenLibException, OverWeightException
    {
        int weight = ElecScaleReadValue(3);
        switch (weight)
        {
            case 0:
                Log.d(TAG, "setBalance: OK.");
                break;
            case -1:
                throw  new EnsenLibException("串口打开失败");
            case -2:
                throw  new EnsenLibException("串口设置失败");
            case -3:
                throw  new EnsenLibException("串口通信异常");
        }
    }

    private OnCardReadSuccess onCardReadSuccess = null;
    public interface OnCardReadSuccess {
        public void onCardReadSuccess(String cardNo);
    }

    public void setCardReadSuccessListener(
            OnCardReadSuccess getScaleValue) {
        onCardReadSuccess = getScaleValue;
    }

    private class ReadCardNumThread extends Thread {

        @Override
        public void run() {
            super.run();
            while (!isStop && !isInterrupted()) {
                int size;
                try {
                    String cardno = CardGetID();
                    if (cardno.equals("-1"))
                    {
                        throw new EnsenLibException("串口打开失败");
                    }
                    else if (cardno.equals("-2"))
                    {
                        throw new EnsenLibException("串口设置失败");
                    }
                    else if (cardno.equals("0"))
                    {
                        continue;
                        //throw new EnsenLibException("串口通信失败");
                    }
                    else if (cardno.equals("-3"))
                    {
                        throw new EnsenLibException("串口通信失败");
                    }
                    else
                    {
                        if(onCardReadSuccess != null)
                        {
                            onCardReadSuccess.onCardReadSuccess(cardno);
                        }
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                    return;
                }
            }
        }


    }

    public void print(String type, String no)
    {
        try{
            String type1 = type;
            if (type.endsWith("\n") == false)
            {
                type1 = type + "\n";
            }

            //向JNI传递字节数组
            byte[] byteArticle = new byte[type1.length()*2];
            byteArticle = type1.getBytes("gbk");

            byte[] byteCode = new byte[no.length()*2];
            byteCode = no.getBytes("gbk");

            Printerprint(byteArticle, byteCode);
        }
        catch (Exception e){
            //Log.i("Hello", "log: getBytes error");
            e.printStackTrace();
        }
    }

    public  void pushRod(String direction)
    {
        if (direction.equals("LEFT"))
        {
            pushRod1Push();
        }
        else if (direction.equals("RIGHT"))
        {
            pushRod2Push();
        }
    }

    public  void pullRod(String direction)
    {
        if (direction.equals("LEFT"))
        {
            pushRod1Pull();
        }
        else if (direction.equals("RIGHT"))
        {
            pushRod2Pull();
        }
    }


    public  void rotate(String direction)
    {
        if (direction.equals("LEFT"))
        {
            motorRun();
        }
        else if (direction.equals("RIGHT"))
        {
            motorRunInvert();
        }
    }

    public  void openLock(int id)
    {
        switch (id)
        {
            case 1:
                lock1Open();
                break;
            case 2:
                lock2Open();;
                break;
            case 3:
                lock3Open();
                break;
            case 4:
                lock4Open();
                break;
            case 5:
                lock5Open();
                break;
            default:
                Log.i(TAG, "openLock: para error , id should be <1,5>. input :" + id);
        }
    }



    /**
     * 采集若干次重量,并求一个平均值.
     * @param times
     * @return 平均值重量,单位g
     */
    private native  int    ElecScaleReadValue(int times);
    private native  int    ElecScaleClearWeight();
    private native  void   lock5Open();
    private native  void   lock4Open();
    private native  void   lock3Open();
    private native  void   lock2Open();
    private native  void   lock1Open();
    private native  void   motorRun();
    private native  void   motorRunInvert();
    private native  void   motorStop();
    private native  void   pushRod1Push();
    private native  void   pushRod1Pull();
    private native  void   pushRod2Push();
    private native  void   pushRod2Pull();

    private native  int    Printerprint(byte[] byteArticle, byte[] byteCode );

    private native String CardGetID();
    private native String IOStatus();






}
